package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Dept;
import top.yongyan0624.pojo.Teacher;
import top.yongyan0624.service.DeptService;
import top.yongyan0624.service.TeacherService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/teacher")
@SessionAttributes("error")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private DeptService deptService;

    @RequestMapping("/toAddTeacherPage")
    public String toAddTeacherPage(Model model){
        List<Dept> deptList = deptService.findAllDept();
        model.addAttribute("deptList",deptList);
        model.addAttribute("error","");

        return "addTeacherPage";
    }

    @RequestMapping("/addTeacher")
    public String addTeacher(Teacher teacher, Model model){
        Teacher c = teacherService.findById(teacher.getTno());
        if (c == null){
            teacherService.addTeacher(teacher);
        } else {
            model.addAttribute("error","该教师编号已被录入！");
            return "addTeacherPage";
        }
        return "redirect:/teacher/toFindAllTeacherPage/1/5";
    }

    @RequestMapping("/deleteTeacher/{id}")
    public String deleteTeacher(@PathVariable("id") Integer id){
        teacherService.deleteTeacher(id);
        return "redirect:/teacher/toFindAllTeacherPage/1/5";
    }

    @RequestMapping("/toUpdateTeacherPage/{id}")
    public String toUpdateTeacherPage(@PathVariable Integer id, Model model){
        Teacher teacher = teacherService.findById(id);
        model.addAttribute("teacher",teacher);
        List<Dept> deptList = deptService.findAllDept();
        model.addAttribute("deptList",deptList);

        return "updateTeacherPage";
    }

    @RequestMapping("/updateTeacher")
    public String updateTeacher(Teacher teacher){
        teacherService.updateTeacher(teacher);

        return "redirect:/teacher/toFindAllTeacherPage/1/5";
    }

    @RequestMapping("/toFindAllTeacherPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,Model model){
        PageInfo<Teacher> pageInfo = teacherService.findAllTeacher(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("teacherList",pageInfo.getList());
        List<Dept> deptList = deptService.findAllDept();
        model.addAttribute("deptList",deptList);
        model.addAttribute("error","");

        return "findAllTeacherPage";
    }


    @RequestMapping("/findTeacher")
    public String findTeacher(Teacher teacher,Model model){
        List<Teacher> teacherList = teacherService.findTeacher(teacher);
        
        if (teacherList.size()<=0){
            model.addAttribute("error","未找到该教师...");

        } else if (teacher.getTno() == null && teacher.getName().equals("")) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("teacherList",teacherList);
            model.addAttribute("error","");
            return "findAllTeacherPage";
        }

        return "redirect:/teacher/toFindAllTeacherPage/1/5";
    }

}
